﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Skipper_Connector.ActivateMethod
{
    public class Licence
    {
        static byte[] keys = new byte[] { 18, 25, 56, 47, 10, 109, 50, 71, 229, 167, 7, 228, 142, 143, 235, 73, 55, 83, 151, 133, 142, 2, 141, 145, 40, 212, 183, 225, 217, 97, 130, 212 };

        public static bool Check()
        {
            if (File.Exists("machine.key"))
            {
                using (FileStream fs = new FileStream("machine.key", FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                {
                    string code = sr.ReadToEnd();

                    if (string.IsNullOrEmpty(code.Trim()))
                    { return false; }

                    try
                    {
                        return checkCode(code);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }

            ActivationWindow activateWindow
                = new ActivationWindow(AESGCM.SimpleEncrypt(getThisMachineCode(), keys, null));
            if (activateWindow.ShowDialog() == DialogResult.OK)
            {
                bool result = checkCode(activateWindow.Code);

                if (result)
                {
                    saveCodeToFile(activateWindow.Code);
                    return true;
                }

                return false;
            }

            return false;
        }

        private static void saveCodeToFile(string p)
        {
            using (FileStream stream = new FileStream("machine.key", FileMode.Create, FileAccess.Write, FileShare.Write))
            using (StreamWriter sw = new StreamWriter(stream, Encoding.Default))
            {
                sw.WriteLine(p);
            }
        }

        private static bool checkCode(string x)
        {
            try
            {
                string b = AESGCM.SimpleDecrypt(x, keys);
                string[] words = b.Split('|');

                if (words.Length != 5)
                { return false; }

                string productName = words[0];
                string cpuid = words[1];
                string diskId = words[2];
                string macId = words[3];
                string status = words[4];

                string machineCode = string.Format("{0}|{1}|{2}|{3}", productName, cpuid, diskId, macId);
                if (machineCode.ToUpper() == getThisMachineCode().ToUpper() && status.ToUpper() == "OK")
                {
                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        private static string getThisMachineCode()
        {
            MachineInfo machine = new MachineInfo();
            string sn = string.Format("{0}|{1}|{2}|{3}",
                                        "MachineConnected",
                                        machine.CpuID(),
                                        machine.DiskID(),
                                        machine.MacAddress());

            return sn;
        }
    }
}
